package cn.thoughtworks.school.services;

import cn.thoughtworks.school.entities.Publisher;
import cn.thoughtworks.school.entities.User;
import cn.thoughtworks.school.repositories.PublisherRepository;
import cn.thoughtworks.school.response.SimplePublisherResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class PublishService {
  @Autowired
  PublisherRepository publisherRepository;

  @Autowired
  private UserCenterService userCenterService;

  public List<SimplePublisherResponse> getPublishersByCurrentOrganizationId(Long userId) {
    Long userCurrentOrganizationId = userCenterService.getUserCurrentOrganizationId(userId);
    List<Publisher> publishers = publisherRepository.findByOrganizationId(userCurrentOrganizationId);
    return simplePublisherResponses(publishers);
  }

  private List<SimplePublisherResponse> simplePublisherResponses(List <Publisher> publishers){
    List<Map> users = getUsers(publishers);
    return publishers.stream()
        .map(publisher -> {
          Map user = users.stream().filter(item -> Objects.equals(publisher.getUserId(),Long.parseLong(item.get("id").toString())))
              .findFirst().get();
          return SimplePublisherResponse.build(user, true);
        })
        .collect(Collectors.toList());

  }

  private List<Map> getUsers(List<Publisher> publishers) {
    List<Long> userIds = publishers.stream()
        .map(Publisher::getUserId)
        .collect(Collectors.toList());

    return userCenterService.getUsersByIds(userIds);
  }

  public List<SimplePublisherResponse> findUserByNameOrEmail(User user,String userNameOrEmail) {
    List<Map> findUsers = userCenterService.getUserByNameOrEmail(userNameOrEmail);

    Long userCurrentOrganizationId = userCenterService.getUserCurrentOrganizationId(user.getId());
    List<Publisher> publishers = publisherRepository.findByOrganizationId(userCurrentOrganizationId);

    return findUsers.stream()
        .map( findUser -> SimplePublisherResponse.build(findUser,isPublisher(findUser,publishers)))
        .collect(Collectors.toList());

  }

  private Boolean isPublisher(Map findUser, List<Publisher> publishers) {
    List<Publisher> publisherList = publishers.stream()
        .filter(publisher -> Objects.equals(publisher.getUserId(), Long.parseLong(findUser.get("id").toString())))
        .collect(Collectors.toList());

    return (publisherList.size() > 0);
  }

  public Publisher addPublisher(Long userId,Long publisherId) {
    Long userCurrentOrganizationId = userCenterService.getUserCurrentOrganizationId(userId);
    Publisher publisher = new Publisher();
    publisher.setUserId(publisherId);
    publisher.setOrganizationId(userCurrentOrganizationId);
    publisher.setAuthorizerId(userId);
    return publisherRepository.save(publisher);
  }

  public void deletePublisher(Long userId){
    Publisher publisher =  publisherRepository.findByUserId(userId);
    publisherRepository.delete(publisher);
  }

  public boolean isPublisherInOrganization(Long userId, long currentOrganizationId) {
    return publisherRepository.findByOrganizationIdAndUserId(currentOrganizationId, userId).isPresent();
  }
}
